package com.dotflat.check.service.impl;

import com.dotflat.check.domain.Card;
import com.dotflat.check.po.CardPO;
import com.dotflat.check.po.SysCardPO;
import com.dotflat.check.repository.CardRepository;
import com.dotflat.check.service.CardService;
import com.dotflat.docs.domain.DocumentDirectory;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class CardServiceImpl extends AreableObjectBaseServiceImpl<Card, CardRepository> implements CardService {

    @Autowired
    @Override
    public void setBaseRepository(CardRepository repository) {
        super.setBaseRepository(repository);
    }

    @Autowired
    private CardRepository cardRepository;

    @Override
    public void insetAll(List<Card> list) {
        cardRepository.saveAll(list);
    }

    @Override
    public Card add(String areaId, String staffId, AreableObjectBasePO param) {
        if (!(param instanceof SysCardPO)){
            return super.add(areaId, staffId, param);
        }
        SysCardPO po = ((SysCardPO)param);
        String cardNo = po.getCardNo();
        Card card = cardRepository.findByCardNo(cardNo);
        if (Objects.nonNull(card)){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "卡号已存在");
        }
        return add(areaId, staffId, param, null);
    }

}